<template>
  <div class="page-common-layout custom-tablePage-container">
    <div class="screen-conditions-container">
      <ScreenConditions :isDataType="false">
        <template slot="conditions">
          <a-select
            class="project-select"
            showSearch
            allowClear
            placeholder="请选择项目"
            :loading="projectLoading"
            :filter-option="filterOption"
            v-model="condition.proCode"
            @change="search()"
          >
            <a-select-option
              :value="item.proCode"
              v-for="item in projectSoucre"
              :key="item.uuid"
            >
              {{ item.proName }}
            </a-select-option>
          </a-select>
        </template>
        <template slot="operate">
          <a-button class="cu-btn-primary" icon="search" @click="search()">
            查询
          </a-button>
          <a-button class="cu-btn-filter" icon="reload" @click="reset()">
            重置
          </a-button>
        </template>
      </ScreenConditions>
    </div>
    <div class="table-body-container">
      <div class="table-operate"></div>
      <div class="table-main">
        <vxe-table
          ref="xTable"
          height="99%"
          align="center"
          show-overflow
          show-header-overflow
          border="inner"
          :data="tableData"
          :column-config="{ resizable: true }"
          size="small"
          :loading="loading"
        >
          <vxe-column type="seq" width="80" title="序号"></vxe-column>
          <vxe-column field="proName" title="项目名称" width="400" align="left">
          </vxe-column>
          <vxe-column field="planStartTime" title="计划开始时间"> </vxe-column>
          <vxe-column field="planEndTime" title="计划结束时间"> </vxe-column>
          <vxe-column field="duration" title="周期（天）"> </vxe-column>
          <vxe-column
            width="120"
            title="合同分解表"
            fixed="right"
            v-if="pageModule == 'scheduleType.03'"
          >
            <template #default="{ row }">
              <div class="files-icon">
                <a-tooltip placement="top">
                  <template slot="title">
                    预览
                  </template>
                  <a-icon
                    type="eye"
                    class="preview-btn"
                    @click="handlePreview(row)"
                  />
                </a-tooltip>
                <a-tooltip placement="top">
                  <template slot="title">
                    下载
                  </template>
                  <a-icon
                    type="download"
                    class="download-btn"
                    @click="handleDownload(row)"
                  />
                </a-tooltip>
              </div>
            </template>
          </vxe-column>
          <vxe-column field="operate" width="120" title="计划表" fixed="right">
            <template #default="{ row }">
              <div class="text-button-group">
                <a-tooltip placement="top">
                  <template slot="title">
                    计划表
                  </template>
                  <a-icon type="file-search" @click="handleWrapPage(row)" />
                </a-tooltip>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="table-pager">
        <vxe-pager
          size="small"
          background
          :current-page.sync="pager.page"
          :page-size.sync="pager.pageSize"
          :total="pager.total"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes']"
          :page-sizes="[20, 50, 100, 500]"
          @page-change="paginationChange"
        >
          <template #left>
            <div class="vxe-pager-left-tip-container">
              共<span>{{ pager.total }}</span
              >条
            </div>
          </template>
        </vxe-pager>
      </div>
    </div>
    <!-- 详情 -->
    <wrapPage
      :key="wrapPage.key"
      :visible.sync="wrapPage.visible"
      :parameter="wrapPage.row"
    ></wrapPage>
    <!-- 附件预览 -->
    <previewModal
      ref="previewModal"
      :visible.sync="previewModal.visible"
      :loading="previewModal.loading"
    ></previewModal>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { _getUUID, downLoadFileByArraybuffer } from "@/utils/util.js";
import { getToken } from "@/utils/TopVUI";
import ScreenConditions from "@/components/extendCustom/ScreenConditions";
import wrapPage from "./wrapPage.vue";
import { getAllProListByQX } from "@/api/common";
import { getPlanControlPage } from "@/api/progressManage";
import { downloadContractDecompose } from "@/api/design";
import { baseUrl } from "@/services/baseUrl";
//单文件预览
import previewModal from "@/pages/document/minio/previewModal";
export default {
  provide() {
    return {
      moduleSource: () => this.pageModule,
    };
  },
  components: {
    ScreenConditions,
    wrapPage,
    previewModal,
  },
  props: {
    pageModule: {
      type: [Number, String],
      default: "",
    },
  },
  data() {
    return {
      //项目
      projectLoading: false,
      projectSoucre: [],
      //查询条件
      condition: {
        //项目
        proCode: undefined,
      },
      //列表
      loading: false,
      tableData: [],
      pager: {
        page: 1,
        pageSize: 50,
        total: 0,
      },
      //详情
      wrapPage: {
        visible: false,
        key: _getUUID(),
        row: {},
      },
      //单文件预览
      previewModal: {
        visible: false,
        loading: false,
      },
    };
  },
  computed: {
    ...mapGetters(["configData", "userInfo"]),
    kkFileView() {
      let index = this.configData.findIndex(
        (item) => item.code === "kkFileView"
      );
      if (index >= 0) {
        return this.configData[index].value;
      } else {
        return "http://192.168.1.216:8012/onlinePreview";
      }
    },
  },
  mounted() {
    Promise.all([this.getProjectData(), this.getTableData()]);
  },
  methods: {
    //合同分解表下载
    handleDownload(row) {
      let { proCode } = row;
      downloadContractDecompose({
        proCode,
      })
        .then((res) => {
          downLoadFileByArraybuffer(res, "合同分解表.xlsx");
        })
        .catch((_) => {});
    },
    //合同分解表预览
    handlePreview(row) {
      let { proCode } = row;
      this.previewModal.loading = true;
      this.$refs.previewModal.type = "file";
      let path = `${baseUrl}/probusiness/contractDecompose/exportData.api?proCode=${proCode}&fullfilename=合同分解表.xlsx`;
      this.$refs.previewModal.fileUrl =
        this.kkFileView +
        "/onlinePreview?url=" +
        encodeURIComponent(Base64.encode(path)) +
        "&watermarkTxt=" +
        //水印内容
        encodeURIComponent("");
      this.previewModal.visible = true;
      this.previewModal.loading = false;
    },

    handleWrapPage(row) {
      this.wrapPage.row = row;
      this.wrapPage.key = _getUUID();
      this.wrapPage.visible = true;
    },

    //查询项目
    getProjectData() {
      this.projectLoading = true;
      return new Promise((resolve, reject) => {
        getAllProListByQX()
          .then((res) => {
            this.projectSoucre = res?.data ?? [];
            resolve();
          })
          .catch((_) => {
            this.projectSoucre = [];
            reject();
          })
          .finally((_) => {
            this.projectLoading = false;
          });
      });
    },
    //查询
    search() {
      this.pager.page = 1;
      this.getTableData();
    },
    //重置
    reset() {
      this.condition.proCode = undefined;
      this.search();
    },

    //查询列表数据
    getTableData() {
      this.loading = true;
      let { page, pageSize } = this.pager;
      let { proCode = "" } = this.condition;
      return new Promise((resolve, reject) => {
        getPlanControlPage({
          page,
          pageSize,
          proCode,
          nodeType: this.pageModule,
        })
          .then((res) => {
            this.tableData = res?.rows || [];
            this.pager.total = res?.totalCount || 0;
            resolve();
          })
          .catch((_) => {
            this.tableData = [];
            this.pager.total = 0;
            reject();
          })
          .finally((_) => {
            this.loading = false;
          });
      });
    },

    //监听分页切换
    paginationChange({ currentPage, pageSize }) {
      this.pager.page = currentPage;
      this.pager.pageSize = pageSize;
      this.getTableData();
    },

    //select 搜索过滤
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },
  },
};
</script>

<style lang="less" scoped></style>
